home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / POPDIAL.C < prev    next >
C/C++ Source or Header  |  1990-06-25  |  11KB  |  382 lines

  1. /*
  2. ** POPDIAL.C
  3. **
  4. ** QC 2.0
  5. */
  6.  
  7. #include <dos.h>
  8. #include <malloc.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <bios.h>
  12. #include <string.h>
  13.  
  14. #define TEST
  15.  
  16. #define NOINT 0x01
  17. #define MSI 0x00
  18. #define THRI 0x02
  19. #define RDAI 0x04
  20. #define RCLS 0x06
  21. #define INT14 0x14
  22. #define CTRL_C 3
  23. #define COM1 0
  24. #define COM2 1
  25. #define BAUD_300 2
  26. #define BAUD_600 3
  27. #define BAUD_1200 4
  28. #define BAUD_2400 5
  29. #define BAUD_4800 6
  30. #define BAUD_9600 7
  31. #define ODD 1
  32. #define NONE 2
  33. #define EVEN 3
  34. #define STOP_1 0
  35. #define STOP_2 1
  36. #define BITS_7 2
  37. #define BITS_8 3
  38. #define _CRET 0x0d
  39.  
  40. #define COMINT_VEC 12                   /* Comm vector interrupt number */
  41. #define COMBUF_SIZE 10240               /* Communications buffer size */
  42. #define IIR     0x3fa                   /* Interrupt Indentification Register */
  43. #define LCR     0x3fb                   /* Line Control Register */
  44. #define MCR     0x3fc                   /* Modem Control Register */
  45. #define MSR     0x3fe                   /* Modem Status Register */
  46. #define LSR     0x3fd                   /* Line Status Register */
  47. #ifdef skip
  48. #define IOR     0x3f8                   /* Communications I/O Register */
  49. #define IER     0x3f9                   /* Interrupt Enable Register */
  50. #define RX_BUFFER 0x3f8                 /* RX buffer (register) */
  51. #endif
  52. #define INTA00  0x20                    /* 8259 Comm board COM1 Register 0 */
  53. #define INTA01  0x21                    /* 8259 Comm board COM1 Register 1 */
  54. #define IMR     0x21                    /* 8259 Interrupt Mask Register */
  55. #define EOI 0x20                        /* 8259 End of Interrupt */
  56. #define peek( addr )        (*(unsigned char far *)addr)
  57.  
  58. struct regval { unsigned int ax,bx,cx,dx,si,di,ds,es; } ;
  59.  
  60.                 /* External Declaractions */
  61.  
  62. char combuf[COMBUF_SIZE];       /* Communications buffer filled by comint */
  63. int cptr,                       /* Index into commuications buffer */
  64.     head,                       /* Current head of commuications buffer */
  65.     popflag,                   /* Online status flag */
  66.     dialflag;
  67. int hotflag;
  68. int hotTimer;
  69. int ticker;
  70. unsigned char bite;
  71.  
  72. int IOR,IER,RX_BUFFER;  /*  ints for defines as test */
  73. int val_array[] = { 0x3f8,0x3f9,0x3f8 };
  74. /* End of test */
  75. union REGS registers;
  76.  
  77. /* Stack and pointer checking off */
  78. #pragma check_stack( off )
  79. #pragma check_pointer( off )
  80.  
  81. static char dialstring[] = "ATDT5358931";
  82. static char dropstring[] = "+++~~ATH";
  83. static int kbval;
  84. unsigned scancode[] = { 56 , 32 };
  85. static unsigned old_IMR, old_IER, old_MCR;
  86.  
  87. /* Prototypes for interrupt functions */
  88. void (interrupt far *oldkey)( void );
  89. void interrupt far newkey( void );
  90. void (interrupt far *oldtimer)( void );
  91. void interrupt far newtimer( void );
  92. void (interrupt far *oldcomm)( void );
  93. void interrupt far newcomm( void );
  94. void (interrupt far *appcomm)( void );
  95.  
  96. /* Variables that will be accessed inside TSR must be global. */
  97. int  fintimer = FALSE, finvideo = FALSE;
  98. int flag = 0;
  99. int waitflag = 0;
  100. int status,oldbyte,newbyte,cntr,_waitctr;
  101. int byte;
  102. int outbyte;
  103. int inbyte;
  104. int row,col;
  105.  
  106. char huge *tsrstack;
  107. char huge *appstack;
  108. char huge *tsrbottom;
  109.  
  110.  
  111. main()
  112. {
  113.     unsigned tsrsize;
  114.     int r;
  115.  
  116.     /* Initialize stack and bottom of program. */
  117.     _asm mov  WORD PTR tsrstack[0], sp
  118.     _asm mov  WORD PTR tsrstack[2], ss
  119.     FP_SEG( tsrbottom ) = _psp;
  120.     FP_OFF( tsrbottom ) = 0;
  121.  
  122.     /* Use 16 paragraph heap (controlled through global in malloc.h). */
  123.     _amblksiz = 256;
  124.  
  125.     /* Program size is:
  126.      *     top of stack
  127.      *   - bottom of program (converted to paragraphs)
  128.      *   + paragraphs in the heap plus
  129.      *   + one extra paragraph just to be safe
  130.      */
  131.     tsrsize = ((tsrstack - tsrbottom) >> 4) + (_amblksiz >> 4) + 1;
  132.  
  133.     /* Replace existing timer and video routines with ours. */
  134.     oldkey = _dos_getvect( 0x09 );
  135.     _dos_setvect( 0x09, newkey );
  136.     oldtimer = _dos_getvect( 0x1c );
  137.     _dos_setvect( 0x1c, newtimer );
  138.     oldcomm = _dos_getvect( 0x14 );
  139.  
  140.     popflag = FALSE;
  141.     hotflag = FALSE;
  142.     hotTimer = 0;
  143.  
  144.     #ifndef TEST
  145.     /* Free the PSP segment and terminate with program resident. */
  146.     _dos_freemem( _psp );
  147.     _dos_keep( 0, tsrsize );
  148.     #endif
  149.     #ifdef TEST
  150.     popdial();
  151.     #endif
  152.  
  153. }
  154.  
  155. void interrupt far newkey()
  156. {
  157.         if (!hotflag)
  158.         {
  159.                 if (inp(0x60) == scancode[0])
  160.                 {
  161.                         hotflag = TRUE;
  162.                 }
  163.         }
  164.         if ( hotflag )
  165.         {
  166.                 if (inp(0x60) == scancode[1])
  167.                 {
  168.                         /* --- reset the keyboard ---- */
  169.                         kbval = inp(0x61);
  170.                         outp(0x61, kbval | 0x80);
  171.                         outp(0x61, kbval);
  172.                         _disable();
  173.                         outp(0x20, 0x20);
  174.                         _enable();
  175.                         /* ---- set hotkey indicator ---- */
  176.                         popflag = TRUE;
  177.                         hotflag = FALSE;
  178.                         return;
  179.                 }
  180.                 else
  181.                         if (inp(0x60) != scancode[0])
  182.                                 hotflag = FALSE;
  183.         }
  184.         _chain_intr( oldkey );
  185. }
  186.  
  187. void interrupt far newtimer()
  188. {
  189.         (*oldtimer)();
  190.         if ( popflag )
  191.         {
  192.                 popflag = FALSE;
  193.                 outp( 0x20 , 0x20 );
  194.                 popdial();
  195.  
  196.         }
  197.         else
  198.         {
  199.                 if ( hotflag )
  200.                 {
  201.                         if ( hotTimer++ == 11 )
  202.                         {
  203.                                 hotflag = FALSE;
  204.                                 hotTimer = 0;
  205.                         }
  206.                 }
  207.                 if( waitflag )
  208.                 {
  209.                         if( _waitctr == 540 )
  210.                                 dialflag = FALSE;
  211.                         else
  212.                                 _waitctr++;
  213.                 }
  214.         }
  215. }
  216.  
  217. popdial()
  218. {
  219.         _disable();
  220.         _asm \
  221.         {
  222.                 mov  WORD PTR appstack[0], sp   ; Save current stack
  223.                 mov  WORD PTR appstack[2], ss
  224.                 mov  sp, WORD PTR tsrstack[0]   ; Load new stack
  225.                 mov  ss, WORD PTR tsrstack[2]
  226.         }
  227.         _enable();
  228.         byte = 0x00;
  229.         dialflag = TRUE;
  230.         popflag = FALSE;
  231.         pause();
  232.         setcom();
  233.         for( ticker = 0; ticker < strlen( dialstring ); ticker++ )
  234.         {
  235.                 outbyte = dialstring[ticker];
  236. RETRY1:
  237.                 pause();
  238.                 status = ( 0x20 & inp( LSR ));
  239.                 if( status != 0x20 )
  240.                         goto RETRY1 ;
  241.                 outp( IOR, outbyte );
  242.                 pause();
  243.                 inbyte = inp( IOR );
  244.         }
  245. RETRY2:
  246.         pause();
  247.         status = ( 0x20 & inp( LSR ));
  248.         if( status != 0x20 )
  249.                 goto RETRY2 ;
  250.         outp( IOR, _CRET );
  251.         pause();
  252.         inbyte = inp( IOR );
  253.         _waitctr = 0;
  254.         waitflag = TRUE;
  255.         while( dialflag )
  256.         {
  257. WAIT1:
  258.                 pause();
  259.                 byte = ( inp( MSR ) & 0x80 );
  260.                 if( byte != 0x80 )
  261.                         goto WAIT1;
  262.                 else
  263.                 {
  264.                         inbyte = inp( IOR );
  265.                         dialflag = FALSE;
  266.                         waitflag = FALSE;
  267.                 }
  268.         }
  269.         if( waitflag )
  270.                 hangup();
  271.         _disable();
  272.         rstcom();
  273.         _asm \
  274.         {
  275.                 mov  sp, WORD PTR appstack[0]
  276.                 mov  ss, WORD PTR appstack[2]
  277.         }
  278.         _enable();
  279. }
  280.  
  281. pause()
  282. {
  283.         for( cntr = 0; cntr < 1 ; cntr++ )
  284.                 flag = TRUE;
  285. }
  286.  
  287. hangup()
  288. {
  289.         for( ticker = 0; ticker < strlen( dropstring ); ticker++ )
  290.         {
  291.                 outbyte = dropstring[ticker];
  292. DROP1:
  293.                 pause();
  294.                 status = ( 0x20 & inp( LSR ));
  295.                 if( status != 0x20 )
  296.                         goto DROP1 ;
  297.                 outp( IOR, outbyte );
  298.                 pause();
  299.                 inbyte = inp( IOR );
  300.         }
  301. DROP2:
  302.         pause();
  303.         status = ( 0x20 & inp( LSR ));
  304.         if( status != 0x20 )
  305.                 goto DROP2 ;
  306.         outp( IOR, _CRET );
  307.         pause();
  308.         inbyte = inp( IOR );
  309.         popflag = TRUE;
  310.         _disable();
  311.         rstcom();
  312.         _asm \
  313.         {
  314.                 mov  sp, WORD PTR appstack[0]
  315.                 mov  ss, WORD PTR appstack[2]
  316.         }
  317.         _enable();
  318. }
  319.  
  320. setcom()
  321. {
  322.         _disable();
  323.         old_IMR = inp(INTA01);
  324.         bite = (0x10 | old_IMR);
  325.         outp(INTA01, bite);                     /* disallow COM1 ints */
  326.         old_IER = inp( IER );
  327.         bite = ( old_IER & 0xf0 );
  328.         outp( IER, bite );
  329.         old_MCR = inp( MCR );
  330.         bite = ( old_MCR & 0x03 );
  331.         outp( MCR, bite );
  332.         bite = inp(MSR);                    /* Clear Modem Status Reg */
  333.         bite = inp(LSR);                    /* Clear Line Status Reg */
  334.         bite = inp(val_array[0]);                   /* Clear I/O Register */
  335.         bite = inp(LCR);
  336.         bite &= 0x7f;
  337.         outp(LCR, bite);                    /* Set DLAB to 0 */
  338.         outp( IOR, 0x00 );
  339.         _enable();
  340. }
  341.  
  342. rstcom()
  343. {
  344. LOOP:
  345.         bite = inp(val_array[0]);                   /* Clear I/O Register */
  346.         bite = inp(LSR);                    /* Clear Line Status Reg */
  347.         if( (bite & 0x01) == 0x01 )
  348.                 goto LOOP;
  349.  
  350.         bite = inp(MSR);                    /* Clear Modem Status Reg */
  351.         outp( MCR, old_MCR );
  352.         outp( IER, old_IER );
  353.         outp(INTA01, old_IMR );
  354.         outp( IOR, _CRET );
  355. }
  356.  
  357.  
  358. static void error_exit    (int errnum);
  359. /* error message table */
  360. static char *error_text[]= {
  361.     NULL,                               /* errnum =  0, no error            */
  362.     NULL,                               /* errnum == 1, windowing error */
  363.     "Syntax:  CXLDEMO [-switches]\n\n"
  364.         "\t -c = CGA snow reduction\n"
  365.         "\t -b = BIOS screen writing\n"
  366.         "\t -m = force monochrome text attributes",
  367.     "Memory allocation error"
  368. };
  369.  
  370. /*---------------------------------------------------------------------------*/
  371. /* this function handles abnormal termination.  If it is passed an  */
  372. /* error code of 1, then it is a windowing system error.  Otherwise */
  373. /* the error message is looked up in the error message table.       */
  374.  
  375. static void error_exit(int errnum)
  376. {
  377.     if(errnum) {
  378.         printf("\n%s\n",(errnum==1)?werrmsg():error_text[errnum]);
  379.         exit(errnum);
  380.     }
  381. }
  382.